<?php

namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\libs\curl;
use yii\db\Query;

class MenuController extends Controller {
	public function actionIndex()
	{
		//配置微信服务器
		    $signature=$_GET["signature"];
		    $timestamp=$_GET["timestamp"];
		    $nonce=$_GET["nonce"];
            $token="weixin";
		    $tmpArr = array($token,$timestamp, $nonce);
		    sort($tmpArr, SORT_STRING);
		    $tmpStr = implode( $tmpArr );
		    $tmpStr = sha1( $tmpStr );

			if($tmpStr==$signature){
			  echo $_GET['echostr'];
			}else{
			return false;
			}
	}
	public function actionGettoken()
	{
         $appID="wxabf6d9a4d65ec549";
         $appsecret="ceaf9421b684a85e560943881d21bf14";
         $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appID."&secret={$appsecret}";
         $res=file_get_contents($url);
         $de_res=json_decode($res,true);
         print_r($de_res);

	}
	public function actionGetmenu()
	{
		$sql="SELECT name,type,`key` FROM menu WHERE pid=0";
		$res=Yii::$app->db->createCommand($sql)->queryAll();
		//print_r($res);die;
		$temp=[];
		foreach ($res as $key => $val) {
			$temp[$key]['type']=$val['type'];
			$temp[$key]['name']=$val['name'];

			if ($val['type']=='view') {
				$temp[$key]['url']=$val['key'];
			}
			else if ($val['type']=='click') {
				$temp[$key]['key']=$val['key'];
			}
		}
		$menu['button']=$temp;
		//print_r($menu);die;
		$token="15_dJjYUj9XSfYxVaZEYTqLoMhdPpL0SdF5L_RM6doAlTA7rR_UPe_q73YTrBLjqeimEmqRqu0dhv0KIVV079WCUVbcf3Tk2tQETc4N1FjIiuGgxABKSYA2D3OGU3uM3I7TQjT10La7xsZT_Z0dHJXcAHAACQ ";
		$url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $token;
		$res = Curl::_post($url, json_encode($menu, JSON_UNESCAPED_UNICODE));
        print_r($res);
        exit;
	}
}